package com.register.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.dbtest.SqlMapClient;
import com.register.beans.RegisterBean;

/**
 * @author bitacademy
 *
 */
/**
 * @author bitacademy
 * 
 */
public class RegisterDAO {
	SqlSession session = null;

	// private Connection connection = null;
	// private PreparedStatement preparedStatement = null;
	// private Statement statement = null;

	public RegisterDAO() {
		// connection = DBConnection.getConnection();
		session = SqlMapClient.getSqlSession();
	}

	public RegisterBean searchMember(String userid, String passwd) {
		ArrayList<RegisterBean> memberInfo = new ArrayList<RegisterBean>();
		RegisterBean bean = new RegisterBean();

		// try {
		// preparedStatement =
		// connection.prepareStatement(QueryCollector.SEARCH_MEMBER_SQL);
		// preparedStatement.setString(1, userid);
		// preparedStatement.setString(2, passwd);
		//
		// ResultSet resultSet = preparedStatement.executeQuery();
		//
		// while (resultSet.next()) {
		// StringBuffer birthDate = new
		// StringBuffer(resultSet.getString("birthdate"));
		// // 20140412
		// birthDate.insert(4, "년 ");
		// birthDate.insert(8, "월 ");
		// birthDate.append("일");
		//
		// bean.setUserName(resultSet.getString("name"));
		// bean.setUserId(resultSet.getString("id"));
		// bean.setPassWd(resultSet.getString("passwd"));
		// bean.setSex(resultSet.getString("sex").equals("f") ? "여" : "남");
		// bean.setBirthDate(birthDate.toString());
		// bean.setCellPhone(resultSet.getString("cellphone"));
		// bean.setEmail(resultSet.getString("email"));
		// bean.setImgFile(resultSet.getString("imgpath"));
		//
		// memberInfo.add(bean);
		// }
		//
		// } catch (SQLException e) {
		// e.printStackTrace();
		// }

		bean.setId(userid);
		bean.setPassWd(passwd);

		List<RegisterBean> sList = session.selectList("mybatis.SEARCH_MEMBER_SQL", bean);
		RegisterBean sBean = new RegisterBean();
		for (RegisterBean s : sList) {
			StringBuffer birthDate = new StringBuffer(s.getBirthDate());
			// 20140412
			birthDate.insert(4, "년 ");
			birthDate.insert(8, "월 ");
			birthDate.append("일");

			sBean.setName(s.getName());
			sBean.setId(s.getId());
			sBean.setPassWd(s.getPassWd());
			sBean.setSex(s.getSex().equals("f") ? "여" : "남");
			sBean.setBirthDate(birthDate.toString());
			sBean.setCellPhone(s.getCellPhone());
			sBean.setEmail(s.getEmail());
			sBean.setImgPath("userImg/" + s.getImgPath());

			memberInfo.add(bean);
		}

		return sBean;
	}

	public JSONArray searchMemberList() {
		JSONArray jArray = new JSONArray();
		List<RegisterBean> sList = session.selectList("mybatis.SEARCH_ALL_MEMBERS_SQL");

		for (RegisterBean s : sList) {
			JSONObject jObject = new JSONObject();
			StringBuffer birthDate = new StringBuffer(s.getBirthDate());
			// 20140412
			birthDate.insert(4, "년 ");
			birthDate.insert(8, "월 ");
			birthDate.append("일");

			jObject.put("name", s.getName());
			jObject.put("id", s.getId());
			jObject.put("passwd", s.getPassWd());
			jObject.put("sex", s.getSex().equals("f") ? "여" : "남");
			jObject.put("birth", birthDate.toString());
			jObject.put("cellphone", s.getCellPhone());
			jObject.put("email", s.getEmail());
			jObject.put("imgpath", s.getImgPath());

			jArray.add(jObject);
		}

		return jArray;
	}

	public int insertMember(RegisterBean registerBean) throws SQLException {
		int insertCnt = 0;
		insertCnt = session.insert("mybatis.INSERT_MEMBER_SQL", registerBean);

		if (insertCnt > 0)
			session.commit();
		else
			session.rollback();

		return insertCnt;
	}

	public boolean checkMember(String userid) {
		boolean isMember = false;
		RegisterBean bean = new RegisterBean();

		// preparedStatement =
		// connection.prepareStatement(QueryCollector.CHECK_MEMBER_SQL);
		// preparedStatement.setString(1, userid);

		// ResultSet resultSet = preparedStatement.executeQuery();

		// while (resultSet.next()) {
		// bean.setId(resultSet.getString("id"));
		// }
		// if (bean.getId() != null) {
		// isMember = true;
		// }
		// bean.setId(userid);
		RegisterBean registerBean = session.selectOne("CHECK_MEMBER_SQL", userid);
		if (registerBean != null) isMember = true;

		return isMember;
	}

	/**
	 * 테스트용 main()
	 */
	public static void main(String[] args) {
		RegisterDAO dao = new RegisterDAO();
		dao.searchMemberList();
	}
}
